﻿using System;
using System.Collections.Generic;
using TeamsPortal.Configuration;

namespace TeamsPortal.Localization
{
    public interface ILocalizer
    {
        string Get(string neutralValue);
        string Get(string neutralValue, params object[] args);
        string this[string neutralValue] { get; }
        int Count { get; }
    }

	public class Localizer : ILocalizer
	{
		private Dictionary<string, string> _translations;

		public Localizer(string cultureFilePath)
		{
            _translations = new Dictionary<string, string>();
            LoadCulture(cultureFilePath);
		}

		private void LoadCulture(string filePath)
		{
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath", "Path to translation file is null");
            }
            _translations = LocalizationParser.ParseFile(filePath);
		}

		public string Get(string neutralValue)
		{
			if (neutralValue == null)
			{
				throw new ArgumentNullException("neutralValue");
			}
			if (_translations.ContainsKey(neutralValue) && !String.IsNullOrEmpty(_translations[neutralValue]))
			{
				return _translations[neutralValue];
			}
			return neutralValue;
		}

		public string Get(string neutralValue, params object[] args)
		{
			return String.Format(Get(neutralValue), args);
		}

		public string this[string neutralValue]
		{
			get
			{
				return Get(neutralValue);
			}
		}

		/// <summary>
		/// Gets the number of loaded translations
		/// </summary>
		public int Count
		{
			get
			{
				return _translations.Count;
			}
		}
    }
}
